package com.framework.service.core.biz.impl;

import com.framework.common.Enums.CertTypeEnum;
import com.framework.common.Enums.UserStatusEnum;
import com.framework.common.Enums.UserTypeEnum;
import com.framework.common.Enums.UserVerifyStatusEnum;
import com.framework.core.api.request.CustomInfoCertReq;
import com.framework.service.core.biz.ICustomInfoBiz;
import com.framework.service.core.dao.mapper.CertInfoMapper;
import com.framework.service.core.dao.mapper.CustomInfoMapper;
import com.framework.service.core.dao.mapper.GuideInfoMapper;
import com.framework.service.core.dao.mapper.ThirdUserInfoMapper;
import com.framework.service.core.dao.model.CertInfo;
import com.framework.service.core.dao.model.CustomInfo;
import com.framework.service.core.dao.model.GuideInfo;
import com.framework.service.core.dao.model.ThirdUserInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author zhoubb
 * @version V1.0
 * @Date 2020/5/4 12:17
 * @since JDK 1.8
 */
@Component
public class CustomInfoBizImpl implements ICustomInfoBiz {

    @Autowired
    private CustomInfoMapper customInfoMapper;

    @Autowired
    private ThirdUserInfoMapper thirdUserInfoMapper;

    @Autowired
    private CertInfoMapper certInfoMapper;

    @Autowired
    private GuideInfoMapper guideInfoMapper;

    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = {Exception.class})
    public boolean certCustomNew(CustomInfoCertReq customInfoCertReq) {
        CustomInfo customInfo = new CustomInfo();
        BeanUtils.copyProperties(customInfoCertReq, customInfo);
        customInfo.setStatus(UserStatusEnum.VALID.getCode());
        customInfoMapper.insertCustomReturnId(customInfo);
        // 更新userId到第三方用户信息表
        ThirdUserInfo thirdUserInfo = new ThirdUserInfo();
        thirdUserInfo.setUserType(customInfoCertReq.getUserType());
        thirdUserInfo.setOpenId(customInfoCertReq.getOpenId());
        thirdUserInfo.setUserId(customInfo.getUserId());
        thirdUserInfoMapper.updateByOpenId(thirdUserInfo);
        // 更新证件信息到证照表
        List<CertInfo> certInfoList = new ArrayList<>();
        CertInfo certInfo = new CertInfo();
        // 身份证正面照
        certInfo.setCertPath(customInfoCertReq.getIdCardFrontPath());
        certInfo.setCertType(CertTypeEnum.ID_CARD_FRONT.getCode());
        certInfo.setSourceId(customInfo.getUserId());
        certInfo.setCreateTime(new Date());
        certInfoList.add(certInfo);

        certInfo = new CertInfo();
        // 身份证反面照
        certInfo.setCertPath(customInfoCertReq.getIdCardBackPath());
        certInfo.setCertType(CertTypeEnum.ID_CARD_BACK.getCode());
        certInfo.setSourceId(customInfo.getUserId());
        certInfo.setCreateTime(new Date());
        certInfoList.add(certInfo);

        certInfo = new CertInfo();
        // 身份证手持照
        certInfo.setCertPath(customInfoCertReq.getIdCardHoldPath());
        certInfo.setCertType(CertTypeEnum.ID_CARD_HOLD.getCode());
        certInfo.setSourceId(customInfo.getUserId());
        certInfo.setCreateTime(new Date());
        certInfoList.add(certInfo);

        if(StringUtils.isNotBlank(customInfoCertReq.getGuideTourPath())) {
            // 导游证
            certInfo = new CertInfo();
            certInfo.setCertPath(customInfoCertReq.getGuideTourPath());
            certInfo.setCertType(CertTypeEnum.GUIDE_TOUR.getCode());
            certInfo.setSourceId(customInfo.getUserId());
            certInfo.setCreateTime(new Date());
            certInfoList.add(certInfo);
        }

        certInfoMapper.insertList(certInfoList);

        // 更新身份信息表
        if(StringUtils.equals(UserTypeEnum.GUIDE.getValue().toString(), customInfoCertReq.getUserType())) {
            //新增或者更新领队信息 状态为认证通过
            GuideInfo guideInfo = guideInfoMapper.selectByPrimaryKey(customInfoCertReq.getUserId());
            if(guideInfo == null) {
                //新增
                guideInfo = getGuideInfo(null, customInfoCertReq);
                guideInfo.setCreateTime(new Date());
                guideInfoMapper.insertSelective(guideInfo);
            } else {
                guideInfo = getGuideInfo(guideInfo, customInfoCertReq);
                guideInfo.setUpdateTime(new Date());
                guideInfoMapper.updateByPrimaryKey(guideInfo);
            }
        }
        return true;
    }

    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = {Exception.class})
    @Override
    public boolean certCustomUpdate(CustomInfoCertReq customInfoCertReq) {
        CustomInfo customInfo = new CustomInfo();
        BeanUtils.copyProperties(customInfoCertReq, customInfo);
        customInfo.setStatus(UserStatusEnum.VALID.getCode());
        customInfoMapper.updateCustomById(customInfo);

        // 更新用户身份到第三方用户信息表
        ThirdUserInfo thirdUserInfo = new ThirdUserInfo();
        thirdUserInfo.setUserType(customInfoCertReq.getUserType());
        thirdUserInfo.setOpenId(customInfoCertReq.getOpenId());
        thirdUserInfoMapper.updateByOpenId(thirdUserInfo);

        // 更新证件信息到证照表
        List<CertInfo> certInfoList = new ArrayList<>();
        CertInfo certInfo = new CertInfo();
        // 身份证正面照
        certInfo.setCertPath(customInfoCertReq.getIdCardFrontPath());
        certInfo.setCertType(CertTypeEnum.ID_CARD_FRONT.getCode());
        certInfo.setSourceId(customInfo.getUserId());
        certInfo.setCreateTime(new Date());
        certInfoList.add(certInfo);

        certInfo = new CertInfo();
        // 身份证反面照
        certInfo.setCertPath(customInfoCertReq.getIdCardBackPath());
        certInfo.setCertType(CertTypeEnum.ID_CARD_BACK.getCode());
        certInfo.setSourceId(customInfo.getUserId());
        certInfo.setCreateTime(new Date());
        certInfoList.add(certInfo);

        certInfo = new CertInfo();
        // 身份证手持照
        certInfo.setCertPath(customInfoCertReq.getIdCardHoldPath());
        certInfo.setCertType(CertTypeEnum.ID_CARD_HOLD.getCode());
        certInfo.setSourceId(customInfo.getUserId());
        certInfo.setCreateTime(new Date());
        certInfoList.add(certInfo);

        if(StringUtils.isNotBlank(customInfoCertReq.getGuideTourPath())) {
            // 导游证
            certInfo = new CertInfo();
            certInfo.setCertPath(customInfoCertReq.getGuideTourPath());
            certInfo.setCertType(CertTypeEnum.GUIDE_TOUR.getCode());
            certInfo.setSourceId(customInfo.getUserId());
            certInfo.setCreateTime(new Date());
            certInfoList.add(certInfo);
        }

        certInfoMapper.insertList(certInfoList);

        // 更新身份信息表
        if(StringUtils.equals(UserTypeEnum.GUIDE.getValue().toString(), customInfoCertReq.getUserType())) {
            //新增或者更新领队信息 状态为认证通过
            GuideInfo guideInfo = guideInfoMapper.selectByPrimaryKey(customInfoCertReq.getUserId());
            if(guideInfo == null) {
                //新增
                guideInfo = getGuideInfo(null, customInfoCertReq);
                guideInfo.setCreateTime(new Date());
                guideInfoMapper.insertSelective(guideInfo);
            } else {
                guideInfo = getGuideInfo(guideInfo, customInfoCertReq);
                guideInfo.setUpdateTime(new Date());
                guideInfoMapper.updateByPrimaryKey(guideInfo);
            }
        }
        return true;
    }

    /**
     * 设置领队信息
     * @param guideInfo
     * @param customInfoCertReq
     * @return
     */
    private GuideInfo getGuideInfo(GuideInfo guideInfo, CustomInfoCertReq customInfoCertReq) {
        if(guideInfo == null) {
            guideInfo = new GuideInfo();
        }
        guideInfo.setGuideId(customInfoCertReq.getUserId());
        guideInfo.setUserId(customInfoCertReq.getUserId());
        guideInfo.setGuideTourId(customInfoCertReq.getGuideTourId());
        guideInfo.setCardNo(customInfoCertReq.getCardNo());
        guideInfo.setRealName(customInfoCertReq.getRealName());
        guideInfo.setCardType(customInfoCertReq.getCardType());
        guideInfo.setCertValidDate(customInfoCertReq.getCertValidDate());
        guideInfo.setStatus(UserVerifyStatusEnum.APPROVED.getCode());
        return guideInfo;
    }
}
